package com.yst.suivi.services.project.versions.lot;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.yst.suivi.comparator.NameObjectComparator;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.project.versions.ProjectVersionUtil;
import com.yst.suivi.data.project.versions.lot.Lot;
import com.yst.suivi.data.project.versions.lot.LotLocalHome;
import com.yst.suivi.data.project.versions.lot.LotUtil;
import com.yst.suivi.services.AbstractSessionBean;
import com.yst.suivi.services.exceptions.BusinessErrorCode;
import com.yst.suivi.services.exceptions.BusinessException;

/**
 * @ejb.bean display-name="LotService" jndi-name="LotHomeRemote" name="LotView"
 *           type="Stateless" view-type="both"
 */
public class LotServiceBean extends AbstractSessionBean implements SessionBean
{
	/**
	 * @throws CreateException
	 * @ejb.create-method view-type = "both"
	 */
	public void ejbCreate() throws CreateException
	{
		// by default nothing to do
	}

	/**
	 * This method create a new Lot.
	 * 
	 * @param standard
	 * @return standard updated
	 * @throws CreateException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public String createLot(String nom, ProjectVersion prj) throws NamingException, CreateException
	{
		checkConstraints(nom, "".intern(), prj);
		LotLocalHome home = LotUtil.getLocalHome();
		Lot lot = home.create();
		updateObject(nom, prj, lot);
		return lot.getId();
	}

	/**
	 * The method checks if all the business constraints are verified.
	 * 
	 * @param login
	 * @param nom
	 * @param prenom
	 */
	private void checkConstraints(String nom, String id, ProjectVersion prj)
	{
		String[] parameter =
		{ "lot", "nom" };
		checkEmptyField(nom, parameter);
		checkFieldLength(nom, SIMPlE_FIELD_MAX_LENGTH, "nom");
		StringBuffer buf = new StringBuffer();
		buf.append(" AND prjVersionId='").append(prj.getId()).append("'");
		checkUnicity(nom, "lot", "nom", id, buf.toString());
	}

	/**
	 * This method update a Lot.
	 * 
	 * @return standard updated
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void storeLot(String id, String nom) throws FinderException, NamingException
	{

		LotLocalHome home = LotUtil.getLocalHome();
		Lot lot = home.findByPrimaryKey(id);
		checkConstraints(nom, id, lot.getProjectVersion());
		updateObject(nom, lot.getProjectVersion(), lot);
	}

	/**
	 * La methode remplie l'objet EJB avec les valeurs fournies
	 * 
	 * @param annee
	 * @param mois
	 * @param jour
	 * @param tpsPos
	 * @param prj
	 */
	private void updateObject(String nom, ProjectVersion prj, Lot lot)
	{
		lot.setNom(nom);
		lot.setProjectVersion(prj);
	}

	/**
	 * This method delete a Lot.
	 * 
	 * @param standard
	 * @throws NamingException
	 * @throws RemoveException
	 * @throws EJBException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void deleteLot(String id) throws NamingException, EJBException, RemoveException
	{
		boolean isLinked = false;
		Connection conn = null;
		try
		{
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup(DB_JNDI_NAME);
			conn = ds.getConnection();
			StringBuffer request = new StringBuffer();
			request.append("select * from activity where lotid='").append(id).append("'");
			PreparedStatement ps = conn.prepareStatement(request.toString());
			ResultSet rs = ps.executeQuery();
			isLinked = rs.next();
			rs.close();
			ps.close();
			conn.close();
		}
		catch (Throwable t)
		{
			isLinked = false;
		}
		if (isLinked)
		{
			throw new BusinessException(BusinessErrorCode.DELETED_LOT_AFFECTED, new String[0]);
		}
		LotLocalHome home = LotUtil.getLocalHome();
		home.remove(id);
	}

	/**
	 * The method returns all the lot of the given project primary key
	 * 
	 * @param prjId
	 * @return
	 * @throws FinderException
	 * @throws NamingException
	 * @ejb.interface-method view-type = "both"
	 */

	public Collection findLot(String prjId) throws FinderException, NamingException
	{
		ProjectVersion prj = ProjectVersionUtil.getLocalHome().findByPrimaryKey(prjId);
		ArrayList retour = new ArrayList(LotUtil.getLocalHome().findByProject(prj));
		Collections.sort(retour, NameObjectComparator.comparator);
		return retour;
	}

	/**
	 * The method returns the lot identify by the primary key
	 * 
	 * @param lotKey
	 * @return
	 * @throws FinderException
	 * @throws NamingException.
	 * @ejb.interface-method view-type = "both"
	 */
	public Lot findLotByKey(String lotKey) throws FinderException, NamingException
	{
		return LotUtil.getLocalHome().findByPrimaryKey(lotKey);
	}
}
